package com.dangf.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dangf.entity.Type;
import com.dangf.service.TypeService;

/**
 * 评论管理 控制器
 * @author Hello_海生
 * @date 2014年3月27日
 */
@Controller
@RequestMapping("/type")
public class TypeController {
	@Autowired
	private TypeService typeService;
	
	//保存分类
	@RequestMapping("/save")
	@ResponseBody
	public Map<String, Object> insert(Type type){
		Map<String, Object> map = new HashMap<String, Object>();
		if(!type.equals("")){
			long id = (Long) typeService.save(type);
			if(id!=0){
				Type t = typeService.findById(id);
				map.put("message", "恭喜你，分类增加成功！");
				map.put("static","success");
				map.put("article",t);
				}else{
					map.put("message", "系统错误，发布失败！");
					map.put("static","error");
				}
		}else{
			map.put("message", "发布失败，请输入分类标题！");
			map.put("static","error");
		}
		return map;
	}
	
	//查询分类信息
	@RequestMapping("/show")
	@ResponseBody
	public Map<String, Object> show(long id){
		Map<String, Object> map = new HashMap<String, Object>();
		if(id!=0){
			Type t= typeService.findById(id);
			if(t!=null){
				map.put("message", "信息查询成功！");
				map.put("static","success");
				map.put("user",t);
			}else{
				map.put("message", "查询失败，此分类 不存在。");
				map.put("static","error");
			}
		}else{
			map.put("message", "查询失败，请输入分类Id");
			map.put("static","error");
		}
		return map;
	}
	
	//查询所有信息
	@RequestMapping("/list")
	@ResponseBody
	public Map<String, Object> list(){
		Map<String, Object> map = new HashMap<String, Object>();
		List<Type> t= typeService.findAll();
		if(t!=null){
			map.put("message", "信息查询成功！");
			map.put("static","success");
			map.put("articles",t);
		}else{
			map.put("message", "查询失败，不知出何错误！");
			map.put("static","error");
		}
		return map;
	}
}
